import java.io.*;

public class MusicPurge 
{
	private static String currentPath = "/Volumes/Public/Shared Music/Davers/Music";
	
	public static void parse(File newFile)
	{
		currentPath = newFile.getPath();
		String[] contents = newFile.list();
		
		//weed out any duplicates in the current directory
		for (String file : contents)
		{
			for (int i = 0; i < contents.length; i++)
			{
				if (file.equalsIgnoreCase(contents[i]))
				{
					//skip, because they're both the same file
				}				
				//check for duplicates with almost the same name
				//be careful of short song names
				else if (file.length() > 6 && contents[i].length() > 4 &&
						file.substring(0, file.length() - 6).equalsIgnoreCase
						(contents[i].substring(0, contents[i].length() - 4)))
				{
					System.out.println(currentPath + "/" + file + " **DELETED**");
					//new File(currentPath + "/" + file).delete();
				}
			}
		}		
		
		//recurse through the file system
		for (int i = 0; i < contents.length; i++)
		{
			String currentFileName = contents[i];
			File currentFile = new File(currentPath + "/" + currentFileName);
			
			if (currentFile.isDirectory())
			{
				parse(currentFile);
				//if we're on the last folder, kick back to parent directory
				if (i == contents.length - 1)
					currentPath = new File(currentPath).getParent();
			}
			else 
			{
				//if we're on the last file, kick back to parent directory
				if (i == contents.length - 1)
					currentPath = new File(currentPath).getParent();
			}				
		}
	}
	
	public static void main(String[] args)
	{
		File startDir = new File(currentPath);
		parse(startDir);
	}
}

